package cn.mdmm.service.impl.weixin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weixin.core.TokenFactory;
import com.weixin.utils.WxWeb;

import cn.mdmm.entity.weixin.WxAccountInfoModel;
import cn.mdmm.mapper.weixin.WxAccountInfoMapper;
import cn.mdmm.service.weixin.WxAccountInfoService;
import cn.mdmm.service.weixin.WxAccountMenuService;
import cn.mdmm.service.weixin.WxAccountReplyRoleService;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：WxAccountInfo对象定义<br>
* 类描述：微信公众号信息业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：dengqiang<br>
* 创建时间：2017-07-05 10:38:15<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/

@Service
public class WxAccountInfoServiceImpl extends BaseServiceImpl<WxAccountInfoModel,Integer> implements WxAccountInfoService{
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	public void setMapper(WxAccountInfoMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}

	@Autowired
	WxAccountReplyRoleService wxAccountReplyRoleService;
	@Autowired
	WxAccountMenuService wxAccountMenuService;
	
	@PostConstruct
	@Override
	public void init() {
		List<WxAccountInfoModel> wxAccountInfos = findModelsByCondition(new HashMap<String, Object>());
		for (WxAccountInfoModel wxAccountInfoModel : wxAccountInfos) {
	    	TokenFactory.addWxConfigs(wxAccountInfoModel.getAccountNo(),wxAccountInfoModel.getAccountId(),wxAccountInfoModel.getAccountSecret());
		}
	}
	
	
	@Override
	public Integer insert(WxAccountInfoModel wxAccountInfoModel) {
		Integer count = 0;
		try {
			/*
			 * 添加公众号，已经添加过就修改，没有添加过就新增
			 */
			WxAccountInfoModel oldWxAccountInfoModel = new WxAccountInfoModel();
			oldWxAccountInfoModel.setAccountNo(wxAccountInfoModel.getAccountNo());
			oldWxAccountInfoModel = super.getObjectByModel(oldWxAccountInfoModel);
			if(oldWxAccountInfoModel != null){
				wxAccountInfoModel.setId(oldWxAccountInfoModel.getId());
				count = super.updateById(wxAccountInfoModel);
			}else{
				wxAccountInfoModel.setStatus(1);
				wxAccountInfoModel.setCreateTime(new Date());
				count = super.insert(wxAccountInfoModel);
			}
			String token = TokenFactory.getToken(TokenFactory.getWxConfig(wxAccountInfoModel.getAccountNo()));
			if(StringUtils.isNotBlank(token)){
				//初始化公众号菜单和自动回复
				wxAccountMenuService.initMenu(wxAccountInfoModel);
				Integer isAutoreplyOpen = wxAccountReplyRoleService.initAutoMsg(wxAccountInfoModel);
				wxAccountInfoModel.setIsAutoreplyOpen(isAutoreplyOpen);
				super.updateById(wxAccountInfoModel);
			}else{
				logger.error("没有获取到token!~~~~~~");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}


	//------------------请在此添加自定义方法（开始）------------------


	@Override
	public void cleanWxAccountMsg(Integer wxAccountId){
		wxAccountMenuService.cleanWxAccountMenu(wxAccountId);
		wxAccountReplyRoleService.cleanWxAccountMsg(wxAccountId);
	}
	
	@Override
	public void initMenu(Integer wxAccountId){
		WxAccountInfoModel  wxAccountInfoModel  = findById(wxAccountId);
		if(wxAccountInfoModel != null){
			wxAccountMenuService.initMenu(wxAccountInfoModel);
		}
	}
	
	@Override
	public void initAutoMsg(Integer wxAccountId){
		WxAccountInfoModel  wxAccountInfoModel  = findById(wxAccountId);
		if(wxAccountInfoModel != null){
			wxAccountReplyRoleService.initAutoMsg(wxAccountInfoModel);
		}
	}
	
	@Override
	public void createWxMenu(Integer wxAccountId){
		WxAccountInfoModel  wxAccountInfoModel  = findById(wxAccountId);
		if(wxAccountInfoModel != null){
			wxAccountMenuService.createWxMenu(wxAccountInfoModel);
		}
	}
	
	@Override
	public void createWxMenu(String wx_no) {
		WxAccountInfoModel  wxAccountInfoModel = new WxAccountInfoModel();
		wxAccountInfoModel.setAccountNo(wx_no);
		wxAccountInfoModel  = getObjectByModel(wxAccountInfoModel);
		if(wxAccountInfoModel != null){
			wxAccountMenuService.createWxMenu(wxAccountInfoModel);
		}
	}

	@Override
	public String getAutlUrl(String wxNo,String redirectUri, String key) {
		return WxWeb.autlUrl(TokenFactory.getWxConfig(wxNo),"snsapi_userinfo",redirectUri, key);
	}
	//------------------请在此添加自定义方法（结束）------------------
	
}
